{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "9f051d16",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy\n",
    "import pandas\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "90db306e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.24.3'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numpy.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29ec5b0d",
   "metadata": {},
   "source": [
    "### IPython魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c67064a6",
   "metadata": {},
   "source": [
    "1. 运行外部python文件 默认是当前目录 也可以使用绝对路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "5d8234bb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello world\n"
     ]
    }
   ],
   "source": [
    "%run helloworld.py"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b6b2649",
   "metadata": {},
   "source": [
    "# ndarray  \n",
    "ndarray是Numpy中表达数据的重要类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "81a758c6",
   "metadata": {},
   "source": [
    "## 创建ndarray数组"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "678950c3",
   "metadata": {},
   "source": [
    "1、使用numpy.array()创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "77325318",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6, 3])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "l = [2,4,6,3]\n",
    "n = numpy.array(l)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8e23f46d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "numpy.ndarray"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f399855e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n.shape # 形状"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8cc543d",
   "metadata": {},
   "source": [
    "2、使用numpy的routines函数创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6716f18d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 1, 1], dtype=int16)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个所有元素都为1的多维数组\n",
    "# 参数说明 shape形状 dtype元素类型 order风格C/F\n",
    "n = numpy.ones(shape=(3,),dtype=numpy.int16)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52964c6c",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1c991ed6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]], dtype=int16)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建一个所有元素都为0的多维数组\n",
    "# 参数说明 shape形状 dtype元素类型 order风格C/F\n",
    "n = numpy.zeros((4,5),dtype = numpy.int16)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6757f7a1",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a45c11e",
   "metadata": {},
   "source": [
    "创建一个所有元素都为指定元素的多维数组\n",
    "- shape 形状\n",
    "- fill_value 填充值\n",
    "- dtype 元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "c61a8612",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[[8, 8, 8, 8],\n",
       "        [8, 8, 8, 8],\n",
       "        [8, 8, 8, 8]],\n",
       "\n",
       "       [[8, 8, 8, 8],\n",
       "        [8, 8, 8, 8],\n",
       "        [8, 8, 8, 8]]], dtype=int16)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.full(shape=(2,3,4),fill_value=8,dtype=numpy.int16)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8df708ac",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6d9841",
   "metadata": {},
   "source": [
    "创建一个主对角线全为1，其它位置为0的二维数组\n",
    "- N 行数\n",
    "- M 列数\n",
    "- k 向右偏移几个位置\n",
    "- dtype 元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e465bbc1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0],\n",
       "       [0, 1, 0],\n",
       "       [0, 0, 1]], dtype=int8)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.eye(N=3,M=3,k=0,dtype=numpy.int8)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94a3c56b",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1034ce89",
   "metadata": {},
   "source": [
    "创建一个等差数列\n",
    "- start 开始值\n",
    "- stop 结束值\n",
    "- num 数列中有多少个数\n",
    "- endpoint 是否包含结束值\n",
    "- retstep 是否返回等差值（步长）\n",
    "- dtype 元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "40627874",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0.,   2.,   4.,   6.,   8.,  10.,  12.,  14.,  16.,  18.,  20.,\n",
       "        22.,  24.,  26.,  28.,  30.,  32.,  34.,  36.,  38.,  40.,  42.,\n",
       "        44.,  46.,  48.,  50.,  52.,  54.,  56.,  58.,  60.,  62.,  64.,\n",
       "        66.,  68.,  70.,  72.,  74.,  76.,  78.,  80.,  82.,  84.,  86.,\n",
       "        88.,  90.,  92.,  94.,  96.,  98., 100.])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 默认情况是包含结束值的\n",
    "n = numpy.linspace(start=0,stop=100,num=51)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7839a21",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c40fb25f",
   "metadata": {},
   "source": [
    "创建一个数值范围的数组 和python中的rang功能类似\n",
    "- start 开始值\n",
    "- stop结束值（不包含）\n",
    "- step步长\n",
    "- dtype元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ad9347c0",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.arange(start=2,stop=10)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cc10c459",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c7abafe0",
   "metadata": {},
   "source": [
    "创建一个随机整数的多维数组\n",
    "- low 最小值\n",
    "- high 最大值\n",
    "- size 数组形状，默认只输出一个随机值\n",
    "- dtype 元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "cbc7e71c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 7, 3],\n",
       "       [7, 7, 9]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 随机整数范围 前闭后开 例 [3,4)\n",
    "n = numpy.random.randint(low=3,high=10,size=(2,3))\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c6e8063",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72c97f84",
   "metadata": {},
   "source": [
    "创建一个服从**标准正态分布**的多维数组\n",
    "- dn 第n个维度的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "be86191c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.24522572, -0.63999885, -0.53066634,  0.26528259,  0.01761572,\n",
       "       -1.49010317,  1.15583082, -1.30892553, -0.25754402,  0.16383247])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randn(10)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fe245d80",
   "metadata": {},
   "source": [
    "创建一个服从**正态分布**的多维数组\n",
    "- loc 均值，对应正态分布的中心\n",
    "- scale 标准差，对应分布的宽度，scale越大，曲线越矮胖，scale越小，曲线越高瘦\n",
    "- size 数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "d3dede7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([101.68031782,  99.57136521,  97.76219388, 100.03070535,\n",
       "       100.46632169, 100.13467754, 100.25466045,  97.96396455,\n",
       "       100.25069809, 100.4700006 ])"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.normal(loc=100,scale=1,size=10)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71d64646",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f3cc529",
   "metadata": {},
   "source": [
    "创建一个元素为0到1（左闭右开）的随机数的多维数组\n",
    "- size 数组形状"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "ba21203d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.70952149, 0.01881849, 0.60486709, 0.88607811, 0.95847171,\n",
       "       0.08642429, 0.09413351, 0.78496408, 0.90133867, 0.602249  ])"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.random(size=10)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c3b62b",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d545dd1",
   "metadata": {},
   "source": [
    "## ndarray常见属性"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b9f8509",
   "metadata": {},
   "source": [
    "- ndim 维度（维度的个数）\n",
    "- shape 形状（各个维度的长度）\n",
    "- size 总长度（其实是总数据量）\n",
    "- dtype 元素类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "1256a308",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('int32')"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randint(low=0,high=10,size=(3,4,5))\n",
    "n.ndim\n",
    "n.shape\n",
    "n.size\n",
    "n.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "decdfc93",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e0976e6",
   "metadata": {},
   "source": [
    "## Numpy的基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "158b86be",
   "metadata": {},
   "source": [
    "1. 索引操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "2aac7da9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 7)"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = [1,2,3,4,5,6,7]\n",
    "i[0],i[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "abdbf631",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 7)"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.array(i)\n",
    "n[0],n[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "46a789ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 5, 3, 2],\n",
       "       [4, 6, 9, 7],\n",
       "       [9, 8, 7, 3],\n",
       "       [9, 2, 6, 2]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i = numpy.random.randint(low=1,high=10,size=(4,4))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "3b751df4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i[1][1]\n",
    "# 简写 三维数据同理\n",
    "i[2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "a15781c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[9, 5, 3, 2],\n",
       "       [1, 2, 3, 4],\n",
       "       [9, 8, 7, 3],\n",
       "       [9, 2, 6, 2]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 修改数组中的数据\n",
    "i[1,2] = 999\n",
    "i[1] = [1,2,3,4]\n",
    "i"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0514c94",
   "metadata": {},
   "source": [
    "2. 切片操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "fd2d4266",
   "metadata": {},
   "outputs": [],
   "source": [
    "i = [1,2,3,4,5,6,7,8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "340108f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([8, 7, 6, 5, 4, 3, 2, 1])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.array(i)\n",
    "# 这里进行切片\n",
    "n[2:6]\n",
    "n[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "0e200e2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[7, 6, 5, 3, 8, 7],\n",
       "       [5, 1, 4, 5, 4, 4],\n",
       "       [5, 9, 1, 9, 3, 8],\n",
       "       [4, 7, 6, 1, 3, 1],\n",
       "       [5, 4, 8, 4, 5, 8],\n",
       "       [5, 9, 3, 4, 7, 8]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = numpy.random.randint(low=1,high=10,size=(6,6))\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "f4055e20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 1, 4, 5, 4, 4],\n",
       "       [5, 9, 1, 9, 3, 8],\n",
       "       [5, 4, 8, 4, 5, 8]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取不连续的多行\n",
    "n[[1,2,4]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "d12fd15d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 5, 5, 4, 5, 5])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取一列\n",
    "n[:,0] # 取所有行，和第0列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "1c4d4263",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 3, 8],\n",
       "       [1, 5, 4],\n",
       "       [9, 9, 3],\n",
       "       [7, 1, 3],\n",
       "       [4, 4, 5],\n",
       "       [9, 4, 7]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取不连续的多列\n",
    "n[:,[1,3,4]]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
